package com.hollycrm.hollysqm.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * 获取属性文件的k/v数据
 * @author jianglong
 *
 */
public class PropertiesUtil {
	private static final String CONFIGNAME="application.properties";
	private static Properties props=new Properties();
	
//	static {
//		props = getProperties();
//	}
	
	public PropertiesUtil(String fileName){
		props = getProperties(fileName);
	}
	
	/**
	 * 加载配置文件
	 * @return
	 */
	public static Properties getProperties(){
		return getProperties(CONFIGNAME);
	}
	
	/**
	 * 加载配置文件
	 * @return
	 */
	public static Properties getProperties(String fileName){
		//Properties props = PropertiesLoaderUtils.loadAllProperties(path);						
		try{
			InputStream inStream=PropertiesUtil.class.getResourceAsStream("/"+fileName);
			props.load(inStream);
			for (Entry<Object,Object> entry : props.entrySet()){
				System.out.println(entry.getKey()+" = "+entry.getValue());
			}
		}catch(IOException ioe){
			ioe.printStackTrace();
		}
		return props;
	}

	/**
	 * 通过Key获取属性Value
	 * @param key
	 * @return
	 */
	public static String getProperty(String key){
		if (props==null){
			props = getProperties();
		}
		//Properties props= getProperties();
		return props.getProperty(key).trim();
	}	
	
	public static void setProperty(String key,String value){
		props.setProperty(key, value);
	}
}
